package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Class.TbClassDto;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbClass;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbCollege;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.Class.ClassAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Class.ClassPag;
import cn.yhbk.web.modules.manager.Pojo.VO.Class.ClassUpdate;
import cn.yhbk.web.modules.manager.Service.ITbClassService;
import cn.yhbk.web.modules.manager.Service.ITbCollegeService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@RestController
@Slf4j
@Api(tags = "班级接口")
@RequestMapping("/manager/tbClass")
public class TbClassController {

    @Autowired
    private ITbClassService iTbClassService;

    @Autowired
    private ITbStudentService studentService;

    @Autowired
    private ITbCollegeService collegeService;

    @ApiOperation("根据ID查询班级")
    @GetMapping("/getClassInfo")
    public CommonResult<TbClassDto> getClassInfo(@RequestParam("id") Long id){
        log.info("查询班级,id:" + id);
        return CommonResult.success(iTbClassService.queryClassInfoById(id));
    }

    @ApiOperation("增加班级")
    @PostMapping("/saveClass")
    public CommonResult<String> saveClass(@RequestBody ClassAdd classAdd){
        log.info("增加班级，" + classAdd);
        TbClass tbClass = BeanUtil.copyProperties(classAdd, TbClass.class);
        Boolean saveClass = iTbClassService.save(tbClass);
        if(!saveClass){
            return CommonResult.failed("保存班级出错");
        }
        return CommonResult.success("保存成功");
    }

    @ApiOperation("修改班级")
    @PutMapping("/updateClass")
    public CommonResult<String> updateClass(@RequestBody ClassUpdate classUpdate){
        log.info("修改班级，" + classUpdate);
        TbClass tbClass = BeanUtil.copyProperties(classUpdate, TbClass.class);
        tbClass.setUpdateTime(LocalDateTime.now());
        Boolean updateClass = iTbClassService.updateById(tbClass);
        if(!updateClass){
            return CommonResult.failed("修改班级出错");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("删除班级")
    @DeleteMapping("/deleteClass/{classId}")
    public CommonResult<String> deleteClass(@PathVariable("classId") Long id){
        //判断班级是否能删除，避免出现脏数据
        int population = studentService.populationForClass(id);
        if(population == 0)return CommonResult.failed("该班级中还有学生，不能删除!");

        log.info("删除班级，id:" + id);
        Boolean deleteClass = iTbClassService.removeById(id);
        if(!deleteClass){
            return CommonResult.failed("删除班级出错");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("分页获取全部班级")
    @GetMapping("/getClassInfoPag")
    public CommonResult<ClassPag> getClassInfoPag(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                  @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize){
        ClassPag classPag = iTbClassService.getClassInfoPag(currentPage, pageSize, null);
        return CommonResult.success(classPag);
    }

    @ApiOperation("根据班级ID查询学生列表")
    @GetMapping("/getClassStudent/{classId}")
    public CommonResult<List<TbStudent>> getClassStudent(@PathVariable("classId") Long classId){
        List<TbStudent> studentList = iTbClassService.getClassStudent(classId);
        return CommonResult.success(studentList);
    }


    @ApiOperation("刷新班级人数")
    @PutMapping("/flush/{classId}")
    public CommonResult flushClassPopulation(@PathVariable Long classId){
        iTbClassService.flushPopulation(classId);

        return CommonResult.failed("刷新成功!");
    }

    @ApiOperation("学工办部分页获取自己学院的全部班级")
    @GetMapping("/getClassInfoStuOffPag")
    public CommonResult<ClassPag> getClassInfoStuOffPag(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                        @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize){
        Long adminId = MyUtil.getCurrentAdminId();
        TbCollege college = collegeService.getByStuOffId(adminId);
        ClassPag classPag = iTbClassService.getClassInfoPag(currentPage, pageSize, college.getId());
        return CommonResult.success(classPag);
    }
}
